package com.masterrabbit.mcslf.controller;

import com.masterrabbit.common.core.utils.uuid.UUID;
import com.masterrabbit.common.core.web.domain.AjaxResult;
import com.masterrabbit.common.security.utils.SecurityUtils;
import com.masterrabbit.mcslf.contans.Constants;
import com.masterrabbit.mcslf.pojo.FinancialRecords;
import com.masterrabbit.mcslf.pojo.MerchantReviews;
import com.masterrabbit.mcslf.pojo.Merchants;
import com.masterrabbit.mcslf.service.FinancialRecordsService;
import com.masterrabbit.mcslf.service.MerchantReviewsService;
import com.masterrabbit.mcslf.service.MerchantsService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.lang.management.ThreadInfo;
import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/merchants")
public class MerchantsController {
    @Autowired
    MerchantsService merchantsService;
    @Autowired
    MerchantReviewsService merchantReviewsService;

    @Autowired
    FinancialRecordsService financialRecordsService;
    @RequestMapping("/getInfoByID")
    public AjaxResult getInfoByID(Long id){
        Merchants byId = merchantsService.getById(id);
        if (byId == null){
            return AjaxResult.success("未得到相关信息");
        }
        System.out.println("sdfffgh");
        return AjaxResult.success(byId);
    }
    @RequestMapping("/add")
    public AjaxResult add(@RequestBody Merchants merchants){
        System.out.println(merchants.toString());
        merchants.setAuditStatus(2);
        Long userId = SecurityUtils.getUserId();
        merchants.setUserId(userId.intValue());
//        todo 送审
//        MerchantReviews merchantReviews = new MerchantReviews();
//        BeanUtils.copyProperties(merchants,merchantReviews);
//        merchantReviewsService.save(merchantReviews);
        merchantsService.save(merchants);
        return AjaxResult.success();
    }
    @RequestMapping("/getuserMerchants")
    public AjaxResult getuserMerchants(HttpServletRequest request){
        Merchants merchants = new Merchants();
        Long userId = SecurityUtils.getUserId();
        merchants.setUserId(userId.intValue());
        List<Merchants> list = merchantsService.getList(merchants);
        return AjaxResult.success(list);
    }
    @RequestMapping("/getstroeMerchants")
    public AjaxResult getstroeMerchants(HttpServletRequest request){
        Merchants currentStore = merchantsService.getById(request.getHeader("currentStore"));
        return AjaxResult.success(currentStore);
    }
//    商家提现
    @RequestMapping("/payouts")
    public AjaxResult Payouts(@RequestBody HashMap<String,Object> map, HttpServletRequest request){
        Merchants currentStore = merchantsService.getById(request.getHeader("currentStore"));
        BigDecimal chngeBlance = new BigDecimal((String) map.get("chngeBlance"));
        //        修改流水信息
        FinancialRecords financialRecords = new FinancialRecords();
        financialRecords.setChangeType(Constants.WITHDRAW);
        financialRecords.setBalanceChange(chngeBlance);
        financialRecords.setMerchantId(Integer.valueOf(currentStore.getMerchantId()));
        financialRecords.setCreateTime(new Date());
        financialRecords.setTitle("提现");
        financialRecordsService.save(financialRecords);
        merchantsService.updataShopBalance(currentStore.getMerchantId(),chngeBlance, Constants.DECLINE);
        return AjaxResult.success();
    }

}
